﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.Utility;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite
{
    public partial class Product : WebSiteBaseicPage
    {
        private readonly ProductBLL _productBll = new ProductBLL();

        protected List<ProductInfo> ProductListDetail = new List<ProductInfo>();

        protected List<ProductInfo> ProductListCat = new List<ProductInfo>();

        public string DivNav = string.Empty;

        public string PDetal = string.Empty;

        protected string Id;

        protected string SelectedCss;

        protected string DisplayCss;

        protected string ProductNav;

        protected void Page_Load(object sender, EventArgs e)
        {
            Id = Request.Params["id"];
            SelectedCss = string.IsNullOrEmpty(Id) ? "" : "class=\"selected\"";
            DisplayCss = string.IsNullOrEmpty(Id) ? "style=\"margin-bottom: 10px;display:block;\"" : "style=\"margin-bottom: 10px;display:none;\"";
            ProductListCat = _productBll.GetProductCatList();
            ProductNav = "<ul class=\"filter option-set\" data-filter-group=\"categories\">";
            var idx = 1;
            var jdx = 1;
            if (idx == 1)
            {
                if (string.IsNullOrEmpty(Id))
                {
                    ProductNav += "<li><a class=\"selected\" href=\"/Product.aspx\">全部</a></li>";
                }
                else
                {
                    ProductNav += "<li><a href=\"/Product.aspx\">全部</a></li>";
                }
            }
            foreach (var row in ProductListCat)
            {
                if (idx == 1 || idx <= 8)
                {
                    if (Id == row.PId.ToString())
                    {
                        ProductNav += "<li><a class=\"selected\" href=\"/Product.aspx?id=" + row.PId + "\">" + row.ProductName + "</a></li>";
                    }
                    else
                    {
                        ProductNav += "<li><a href=\"/Product.aspx?id=" + row.PId + "\">" + row.ProductName + "</a></li>";
                    }
                    if (idx % 8 == 0)
                    {
                        ProductNav += "</ul><ul class=\"filter option-set\" data-filter-group=\"categories\">";
                    }
                }
                if (idx > 8)
                {
                    if (Id == row.PId.ToString())
                    {
                        ProductNav += "<li><a class=\"selected\" href=\"/Product.aspx?id=" + row.PId + "\">" + row.ProductName + "</a></li>";
                    }
                    else
                    {
                        ProductNav += "<li><a href=\"/Product.aspx?id=" + row.PId + "\">" + row.ProductName + "</a></li>";
                    }
                    if (jdx % 9 == 0)
                    {
                        ProductNav += "</ul><ul class=\"filter option-set\" data-filter-group=\"categories\">";
                    }
                    jdx++;
                }
                idx++;
            }

            if (string.IsNullOrEmpty(Id))
            {
                LoadData(null, null);
            }
            else
            {
                if (RequestCheck.CheckKeyWord(Id))
                {
                    Response.Redirect("Product.aspx");
                }
                else
                {
                    LoadData(null, null);
                }
            }
            
        }

        public void LoadData(object sender, EventArgs e)
        {
            Pagination1.PageIndex = sender != null ? Convert.ToInt32(((LinkButton)sender).CommandArgument) : 1;
            var recordcount = _productBll.GetTotal(Id);
            Pagination1.PageSize = 5;
            ProductListDetail = _productBll.GetProductList(Id, Pagination1.PageIndex, Pagination1.PageSize);
            Pagination1.RecordCount = recordcount;
        }
    }
}